<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field['options'].displayStyle"
  >
    <div class="scene-DRC-double-patient-info-widget" :style="getStyle('head')">
      <!--科室信息-->
      <div
        class="dept-room"
        :class="getDeptClassName"
        v-if="!optionsInfo.deptHide || !optionsInfo.roomHide"
      >
        <p class="room" v-if="!optionsInfo.roomHide">
          <NoticeBar
            :text="
              getRoomDeptInfo[optionsInfo['roomNameKey']] ||
              getRoomDeptInfo.roomName ||
              '\u00a0'
            "
          />
        </p>
        <p class="dept" v-if="!optionsInfo.deptHide">
          <NoticeBar :text="getRoomDeptInfo.deptName || '\u00a0'" />
        </p>
      </div>
      <!--医生信息-->
      <div
        :class="index=== 0?'doctor-info ':'doctor-info  margTop' "
        v-for="(item, index) in getRoomDoctorInfo"
        :key="item.workerId || index"
      >
        <!--{{item}}-->
        <div class="doctorMssageBox">
          <div class="picture flex-box-left">
            <img :src="getDoctorPicture(item)" alt="医生" />
          </div>
          <div class="info flex-box-right">
            <div class="name-level">
              <div class="workerNameBox ">
                <div class="workerNameTitle">医生</div>
                <div class="workerNameLuRu">
                 <ScrollX> {{ item["workerName"] || "\u00a0" }}</ScrollX>
                </div>
              </div>
              <div class="workerNameBox margTop">
                <div class="workerNameTitle">职称</div>
                <div class="workerNameLuRu">
                  <ScrollX>{{ item.workerTitle || "\u00a0" }}</ScrollX>
                </div>
              </div>
              <!-- <p class="name">{{ item["workerName"] || "\u00a0" }}</p> -->
              <!-- <p class="level" v-if="item['workerTitle']">
                {{ item.workerTitle || "\u00a0" }}
              </p> -->
            </div>
            <!-- <div class="patient-number" v-if="optionsInfo.deptHide">
              <ScrollX :key="`${item.workerId}-${index}}`">
                {{ getParentRegisterObjName(item) || "\u00a0" }}
              </ScrollX>
            </div> -->
          </div>
        </div>

        <!--患者信息-->
        <div class="patient-info" :class="getParentNumberHide" v-if="true">
          <div
            class="item current-patient flex-box"
            :class="getPatientClassName('current-patient')"
          >
            <div class="title flex-box-left">就诊</div>
            <div
              class="content flex-box-right"
              :class="{ 'current-patient-info': scrollInfo(item) }"
            >
              <template v-if="true">
                <div class="scroll-info" v-if="scrollInfo(item)">
                  <vue-seamless-scroll
                    class="scroll"
                    :ref="`scroll-${item['workerId']}`"
                    @ScrollEnd="(e) => handleScrollEnd(e, item)"
                    :class-option="optionScroll2"
                    :data="item['treatingPatient']"
                  >
                    <template v-for="PatientItem in item['treatingPatient']">
                      <p class="item">
                        {{ PatientItem.orderNo || "\u00a0" }}
                        {{ PatientItem.patientName || "\u00a0" }}
                      </p>
                    </template>
                  </vue-seamless-scroll>
                </div>
                <p v-else>
                  <template v-for="PatientItem in item['treatingPatient']">
                    {{ PatientItem.orderNo || "\u00a0" }}
                    {{ PatientItem.patientName || "\u00a0" }}
                  </template>
                </p>
              </template>
            </div>
          </div>
          <div
            class="item wait-patient flex-box"
            :class="getPatientClassName('wait-patient')"
          >
            <div class="title flex-box-left">待诊</div>
            <div class="content flex-box-right">
              <p
                v-for="item2 in getWaitingPatientList(
                  item['waitingPatientList']
                )"
                :key="item2.patientIdNo"
              >
                {{ item2.orderNo || "\u00a0" }}
                {{ item2.patientName || "\u00a0" }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import { NoticeBar } from "vant";
import emitter from "@/utils/designer/emitter";
import { pxToVw } from "@/utils/common";
import vueSeamlessScroll from "vue-seamless-scroll";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import { buildParentApi } from "@/utils/designer/util";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-double-patient-info-5Row-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollX,
    NoticeBar,
    CompOperate,
    vueSeamlessScroll,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi(),
    },
  },

  data() {
    return {
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    };
  },

  computed: {
    optionScroll2() {
      return {
        hoverStop: true,
        direction: 1, // 0向下 1向上 2向左 3向右
        isSingleRemUnit: true,
        step: [1, 1.5, 2, 2.5, 3][0], // 数值越大速度滚动越快
        limitMoveNum: 2, // 开始无缝滚动的数据量 this.dataList.length
      };
    },

    // 判断当前患者是否滚动
    scrollInfo() {
      return function (info) {
        return !!info.treatingPatient && info.treatingPatient.length > 1;
      };
    },
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.optionsInfo || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.headHeight));
            Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            return Obj;
          case "picture":
            Reflect.set(Obj, "height", pxToVw(info.imageHeight));
            Reflect.set(Obj, "width", pxToVw(info.imageWidth));
            Reflect.set(Obj, "borderRadius", pxToVw(info.imageRadius));
            return Obj;
        }
      };
    },

    // 获取当前患者号别或原科室
    getParentRegisterObjName() {
      return function (rows) {
        const self = this;
        let {
          deptHide,
          registerObjNameKey = "registerObjName",
          registerSource = 2,
        } = self.optionsInfo;
        if (deptHide) {
          if (!!rows?.workerId) {
            if (parseInt(registerSource) === 2) {
              let patientList = Array();
              if (rows?.treatingPatient?.length) {
                patientList = rows.treatingPatient;
              } else if (rows?.preTreatingPatient?.length) {
                patientList = rows.preTreatingPatient;
              }
              if (patientList?.length) {
                let registerObjNameInfo = Array();
                patientList.forEach((item) => {
                  let temp =
                    item[registerObjNameKey] || item["registerObjName"];
                  if (!!temp) {
                    registerObjNameInfo.push(temp);
                  }
                });
                // 去重
                let registerObjName = self.$lodash.compact(registerObjNameInfo);
                if (!!registerObjName && !!registerObjName.length) {
                  return self.$lodash.uniq(registerObjName).join(",");
                }
              } else {
                return "\u00a0";
              }
            } else {
              let roomDeptInfo = this.getRoomDeptInfo;
              return roomDeptInfo?.registerObjName || "\u00a0";
            }
          } else {
            return `\u00a0`;
          }
        }
      };
    },

    // 获取医生头像
    getDoctorPicture() {
      return function (row) {
        let isPause = row.isPause;
        let header = "/tpleditor/resource/assets/images/header.png";
        if (!isPause) {
          return row.workerPicture || header;
        } else {
          return this.pause;
        }
      };
    },

    // 科室隐藏
    getDeptClassName() {
      let { deptHide } = this.optionsInfo;
      if (deptHide) {
        return "dept-room-hide";
      }
    },

    // 隐藏科室--显示当前号别--患者区域的变化
    getParentNumberHide() {
      let { deptHide } = this.optionsInfo;
      if (!deptHide) {
        return "patient-info-hide";
      }
    },

    // 设置不同模式下的样式
    getPatientClassName() {
      let { waitMode } = this.optionsInfo;
      return function (type) {
        if (type === "wait-patient") {
          switch (waitMode) {
            case 1:
              return "current-patient";
            case 2:
              return "current-patient2";
            case 3:
              return "current-patient3";
          }
        } else if (type === "wait-patient") {
          switch (waitMode) {
            case 1:
              return "wait-patient";
            case 2:
              return "wait-patient2";
            case 3:
              return "wait-patient3";
          }
        }
      };
    },

    getDoctorClassName() {
      let { deptHide, roomHide } = this.optionsInfo;
      if (deptHide && roomHide) {
        return "doctor-info-2";
      } else if (deptHide || roomHide) {
        return "doctor-info-1";
      } else {
        return "doctor-info";
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDeptInfo"];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let temp = Array();
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDoctorInfo"];
      if (devType) {
        if (!!roomDoctorInfo.apiData?.length) {
          if (roomDoctorInfo.apiData.length >= 2) {
            return roomDoctorInfo.apiData.slice(0, 2);
          } else {
            let arr = new Array(2 - roomDoctorInfo.apiData.length).fill({});
            return [...roomDoctorInfo.apiData, ...arr];
          }
        } else {
          return temp.push({}, {});
        }
      } else {
        if (!!roomDoctorInfo.mockData?.length) {
          if (roomDoctorInfo.mockData.length >= 2) {
            return roomDoctorInfo.mockData.slice(0, 2);
          } else {
            let arr = new Array(2 - roomDoctorInfo.mockData.length).fill({});
            return [...roomDoctorInfo.mockData, ...arr];
          }
        } else {
          return temp.push({}, {});
        }
      }
    },

    // 候诊患者截取
    getWaitingPatientList() {
      return function (row) {
        if (!!row && !!row.length) {
          let { waitMode } = this.optionsInfo;
          return row.slice(0, waitMode);
        } else {
          return [];
        }
      };
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      // this.parentApi.executeParentMethod('writeLog', JSON.stringify({sign:'模板日志',key:key,data:data}));
      switch (key) {
        case "roomDeptInfo":
          console.log(`科室信息-${data}`);
          this.states = true;
          buildParent.executeParentMethod(
            "writeLog",
            JSON.stringify({
              sign: "模板日志-科室信息",
              key: key,
              data: data,
            })
          );
          this.$store.dispatch("setRoomDeptInfo", data);
          break;
        case "roomDoctorInfo":
          console.log(`全量医患信息-${data}`);
          this.states = true;
          buildParent.executeParentMethod(
            "writeLog",
            JSON.stringify({
              sign: "模板日志-医生信息",
              key: key,
              data: data,
            })
          );
          this.$store.dispatch(
            "setRoomDoctorInfo",
            this.$lodash.cloneDeep(data)
          );
          break;
      }
    },
    // 滚动结束
    handleScrollEnd() {},
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        this.optionsInfo = val.options.DRCDoublePatientInfoConfig;
      },
      deep: true,
      immediate: true,
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-DRC-double-patient-info-widget {
  padding: 28px 30px;
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  color: #333;
  height: 150px;
  font-size: 40px;
  background: transparent;
  overflow: hidden;
  .workerNameBox {
    display: flex;
    font-size: 64px;
  }
  .margTop{
    margin-top: 32px;
  }
  .workerNameTitle {
    width: 194px;
    height: 120px;
    background: #0059b3;
    color: white;
    border-radius: 20px 0 0 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .workerNameLuRu {
    width: 390px;
    height: 120px;
    background: #ccdef0;
    border-radius: 0 20px 20px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 600;
  }
  .doctorMssageBox {
    display: flex;
    background: white;
    padding: 30px;
    border-radius: 20px;
  }
  //患者信息
  .patient-info {
    display: flex;
    justify-content: space-between;
    .item {
      display: flex;
      overflow: hidden;
      border-radius: 12px;
      width: auto;
      min-width: auto;

      .title {
        color: white;
        align-items: center;
        align-self: center;
        text-align: center;
        padding: 0 30px;
        height: 100%;
        flex: inherit;
        display: flex;
        width: 124px !important;
        font-size: 54px;
        line-height: 66px;
        border: none;
      }

      .content {
        display: flex;
        align-items: center;
        flex-direction: column;
        align-self: center;
        justify-content: left;
        background: transparent;
        border: none;
        width: calc(100% - 124px) !important;
        
        p {
          font-size: 60px;
          padding-left: 20px;
          line-height: 86px;
          text-align: left;
          width: 100%;
          font-weight: 600;
        }
      }
    }

    .current-patient {
      width: 491px;
      height: 170px;
      margin-top: 30px;
      min-height: 170px;
      background: rgba(0, 89, 179, 0.2);
      border-radius: 20px;
      .title {
        background: rgba(0, 89, 179, 1);
      }

      p {
        color: rgba(0, 89, 179, 1);
      }
    }

    .current-patient-info {
      height: 100%;
      display: flex;
      align-self: center;
      align-items: center;

      .scroll-info {
        width: 100%;
        overflow: hidden;
        margin-top: 10px;
        height: calc(100% - 20px);
        box-sizing: border-box;

        p {
          height: 100%;
          width: 100%;
          display: flex;
          font-size: 60px;
          line-height: 1.5;
          align-items: flex-start;
        }
      }
    }

    .wait-patient {
      width: 491px;
      height: 170px;
      margin-top: 30px;
      min-width: auto;
      background: rgba(255, 198, 85, 0.2);
      border-radius: 20px;
      .title {
        background: rgba(229, 134, 2, 1);
      }

      p {
        color: rgba(229, 134, 2, 1);
        line-height: 106px !important;
      }
    }
  }
  //科室
  .dept-room {
    padding: 20px 0 8px 0;
    background: #0059b3;
    height: 307px;
    border-radius: 20px;
    overflow: hidden;
    display: flex;
    p {
      color: #ffffff;
      text-align: center;
      font-size: 80px;
      padding: 0 30px;
    }

    .dept {
      font-size: 80px;
      line-height: 120px;
      width: 100%;
      max-width: 600px;
      // @include singleEllipsis(100%);

      :deep(.van-notice-bar) {
        @include fontWeight();
        background: transparent;
        height: 100%;
        width: 100%;
        font-size: 44px;

        .van-notice-bar__wrap {
          height: 100%;
          margin: 0 auto;
          display: flex;
          min-height: 120px;
          justify-content: center;
          @include fontWeight();
          color: #333333;
          font-size: 44px;
          overflow: hidden;
          width: 100%;
          text-align: center;

          > div {
            font-size: 80px;
            font-weight: normal;
            line-height: 120px;
            color: white;
            text-align: center;
          }
        }
      }
    }

    .room {
      font-size: 120px;
      font-weight: normal;
      line-height: 180px; 
      width: 100%;
      max-width: 245px;
      //诊室滚动
      :deep(.van-notice-bar) {
        @include fontWeight();
        background: transparent;
        height: 100%;
        width: 100%;
        font-size: 44px;

        .van-notice-bar__wrap {
          height: 100%;
          margin: 0 auto;
          min-height: 170px;
          @include fontWeight();
          color: #333333;
          font-size: 44px;
          overflow: hidden;
          width: 100%;
          text-align: center;
          display: flex;
          justify-content: center;

          > div {
            font-size: 120px;
            font-weight: normal;
            line-height: 180px;
            color: white;
            text-align: center;
          }
        }
      }
    }
  }

  .dept-room-hide {
    display: flex;

    p {
      width: 100%;
      align-self: center;
    }
  }

  //医生信息
  .doctor-info {
    display: flex;
    flex-direction: column;
    margin: 30px 0;
    min-width: auto;
    // max-height: 526px;

    .picture {
      flex: inherit;
      border-radius: 20px;
      overflow: hidden;
      flex-basis: 251px;
      min-width: 251px;
      border: none;
      height: 340px;
      min-height: 340px;
      align-self: flex-end;
      background: transparent;
      img {
        width: 100%;
      }
    }

    .info {
      flex: inherit;
      display: flex;
      flex-direction: column;
      width: 100%;
      // width: calc(100% - 398px - 34px);
      border: none;
      // height: 100%;
      // align-self: flex-end;
      background: transparent;
      justify-content: center;
      align-items: center;
      .name-level {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 100%;
        width: 100%;
        .name {
          font-size: 72px;
          line-height: 80px;
          height: 80px;
          @include fontWeight();
          @include singleEllipsis(100%);
          width: auto;
          // max-width: inherit;
        }

        .level {
          font-size: 30px;
          color: #e8e8e8;
          flex-basis: auto;
          flex-shrink: 0;
          line-height: 60px;
          border-radius: 8px;
          padding: 4px 20px;
          height: 60px;
          overflow: hidden;
          margin-left: 30px;
          display: inline-block;
          align-self: self-end;
          background: #0059b3;
        }
      }

      .patient-number {
        font-family: PingFangSC, PingFang SC, serif;
        font-weight: 450;
        font-size: 54px;
        color: #0059b3;
        line-height: 78px;
        padding: 12px 0;
        //诊室滚动
        :deep(.van-notice-bar) {
          @include fontWeight();
          background: transparent;
          height: 100%;
          width: 100%;
          font-size: 44px;

          .van-notice-bar__wrap {
            height: 100%;
            margin: 0 auto;
            min-height: 80px;
            @include fontWeight();
            overflow: hidden;
            width: 100%;
            text-align: left;
            display: flex;
            justify-content: left;

            > div {
              font-size: 54px;
              font-weight: normal;
              height: 100%;
              display: flex;
              align-self: center;
              align-items: center;
              color: #0059b3;
              text-align: center;
            }
          }
        }
        .scrollText {
          text-align: left;
          margin: initial;
          :deep(.st-inner) {
            margin: initial;
          }
        }
      }

      .patient-info-hide {
        > .item {
          height: 182px;

          .title {
            font-size: 60px;
            line-height: 70px;
          }

          &:first-child {
            margin-top: 42px;
          }
        }
      }
    }
  }

  .doctor-info-1 {
    margin: 118px 0 0 0;
  }
}
</style>
